package com.leoren.run.activity.run.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.leoren.run.R;
import com.leoren.run.activity.share.GenSharePicActivity;
import com.leoren.run.common.Const;
import com.leoren.run.dbbean.UserDB;
import com.leoren.run.utils.DateParseUtil;
import com.leoren.run.utils.GsonUtil;
import com.leoren.run.utils.MyNumberUtils;
import com.leoren.run.vo.RunRecordVo;
import com.leoren.run.vo.ShareDataVo;

import org.litepal.LitePal;

import java.util.Date;
import java.util.List;

/**
 * RunFragment展示地图列表的适配器
 */
public class RunRecordAdapter extends RecyclerView.Adapter<RunRecordAdapter.RunRecordHolder> {

    private Context context;
    private List<RunRecordVo> recordVos;

    public RunRecordAdapter(Context context, List<RunRecordVo> recordVos) {
        this.context = context;
        this.recordVos = recordVos;
    }

    static class RunRecordHolder extends RecyclerView.ViewHolder{
        LinearLayout itemRecordView;
        ImageView pathImg;
        TextView distanceText;
        TextView timeText;
        TextView dateText;

        public RunRecordHolder(@NonNull View itemView) {
            super(itemView);
            itemRecordView = (LinearLayout) itemView;
            pathImg = itemRecordView.findViewById(R.id.item_record_pic_img);
            distanceText = itemRecordView.findViewById(R.id.item_record_distance);
            timeText = itemRecordView.findViewById(R.id.item_record_time);
            dateText = itemRecordView.findViewById(R.id.item_record_date);
        }
    }


    @NonNull
    @Override
    public RunRecordHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_record_layout, viewGroup, false);
        return new RunRecordHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RunRecordHolder holder, int i) {
        RunRecordVo vo = recordVos.get(i);
        Glide.with(context).load(vo.getPicUrl()).into(holder.pathImg);
        holder.distanceText.setText(vo.getDistance() + "");
        holder.timeText.setText(DateParseUtil.dateFromIntMin(vo.getTime()));
        holder.dateText.setText(vo.getDate());
        final ShareDataVo shareDataVo = getShareVo(holder, vo);
        holder.itemRecordView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, GenSharePicActivity.class);
                intent.putExtra(Const.START_SHARE, GsonUtil.GenerateJsonStr(shareDataVo));
                context.startActivity(intent);
            }
        });
    }

    /**
     * 生成分享的一个实体类
     * @param holder
     * @param recordVo
     * @return
     */
    private ShareDataVo getShareVo(RunRecordHolder holder, RunRecordVo recordVo){
        ShareDataVo vo = new ShareDataVo();
        UserDB db = LitePal.findFirst(UserDB.class);
        vo.setUserId(db.getTagId());
        vo.setDateTime(recordVo.getDate());
        vo.setDistance(MyNumberUtils.getDistanceKMFromM(recordVo.getDistance()));
        vo.setTime(holder.timeText.getText().toString().trim());
        vo.setBackUrl(recordVo.getPicUrl());
        vo.setUsername(db.getUsername());
        vo.setHeadUrl(db.getFaceImage());
        return vo;
    }

    @Override
    public int getItemCount() {
        return recordVos.size();
    }




}
